package com.enjoyor.soa.traffic.core.efoms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.efoms.domain.EvalGradeInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.FileInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.WorkordersInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.WorkordersRecordInfo;
import com.enjoyor.soa.traffic.core.efoms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.core.efoms.helper.GetObjectHelper;
import com.enjoyor.soa.traffic.core.efoms.mapper.IEvalGradeInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.mapper.IFileInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.mapper.IWorkordersInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.mapper.IWorkordersRecordMapper;
import com.enjoyor.soa.traffic.core.efoms.service.IWorkordersRecordService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;


@Service
@Transactional
@SuppressWarnings("rawtypes")
public class WorkordersRecordService implements IWorkordersRecordService {

    @Autowired
    private IWorkordersRecordMapper workordersRecordMapper;

    @Autowired
    private IWorkordersInfoMapper workordersInfoMapper;
    
    @Autowired
    private IFileInfoMapper fileInfoMapper;
    
    @Autowired
    private IEvalGradeInfoMapper evalGradeInfoMapper;
    
    @Autowired
    private IPubLogMapper pubLogMapper;

    @Override
    public WorkordersRecordInfo selectWorkordersRecordById(String workordersRecordId){
        return workordersRecordMapper.selectById(workordersRecordId);
    }
    
    @Override
	public List<WorkordersRecordInfo> selectWorkordersRecordList(String workordersRecordId, String workordersId,
			String operTypeCode, String operResultCode, String operStartDate, String operEndDate){
    	try {
	    	Map<String,Object> paramT = new HashMap<String ,Object>();
	    	paramT.put("workordersRecordId", workordersRecordId);
	    	paramT.put("workordersId", workordersId);
	    	paramT.put("operTypeCode", operTypeCode);
	    	paramT.put("operResultCode", operResultCode);
	    	paramT.put("operStartDate", operStartDate);
	    	paramT.put("operEndDate", operEndDate);
	    	return workordersRecordMapper.selectWorkordersRecordList(paramT);
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
	
    @Override
    public Page selectWorkordersRecordPage(Page page, String workordersRecordId, String workordersId,
			String operTypeCode, String operResultCode, String operStartDate, String operEndDate){
    	try {
	    	Map<String,String> mapBean = new HashMap<String,String>();
	    	mapBean.put("workordersRecordId", workordersRecordId);
	    	mapBean.put("workordersId", workordersId);
	    	mapBean.put("operTypeCode", operTypeCode);
	    	mapBean.put("operResultCode", operResultCode);
	    	mapBean.put("operStartDate", operStartDate);
	    	mapBean.put("operEndDate", operEndDate);
	    	page.setMapBean(mapBean);
	        return workordersRecordMapper.selectPageInfo(page);
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
    
    @Override
    public String insertWorkordersRecord(WorkordersRecordInfo workordersRecord, String personName){
    	try {
	    	// 默认操作记录信息
    		String workordersId = workordersRecord.getWorkordersId();
	        String workordersRecordId = "WR" + TimeHelper.getUniqueString();
	        workordersRecord.setWorkordersRecordId(workordersRecordId);
	        workordersRecord.setOperPerson(personName);
	        workordersRecord.setOperDate(TimeHelper.dateToLongString());
	        workordersRecordMapper.insert(workordersRecord);
	    	//新增关联附件
	        List<FileInfo> fileInfoList = workordersRecord.getFileInfoList();
	    	if(null != fileInfoList && fileInfoList.size() > 0) {
	    		for(FileInfo fileInfo : fileInfoList) {
	        		String fileId = "F" + TimeHelper.getUniqueString();
	        		fileInfo.setFileId(fileId);
	        		fileInfo.setReleteId(workordersRecordId);
	        		fileInfoMapper.insert(fileInfo);
	        	}
	    	}
	    	//新增关联附件
	        List<EvalGradeInfo> evalGradeList = workordersRecord.getEvalGradeList();
	        if(null != evalGradeList && evalGradeList.size() > 0) {
	    		for(EvalGradeInfo evalGradeInfo : evalGradeList) {
	        		String evalGradeId = "EG" + TimeHelper.getUniqueString();
	        		evalGradeInfo.setEvalGradeId(evalGradeId);
	        		evalGradeInfo.setWorkordersId(workordersId);
	        		evalGradeInfoMapper.insert(evalGradeInfo);
	        	}
	    	}
	        pubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
	                String.format("添加维修记录编号:{%s} 操作成功!", new Object[] { workordersRecordId }),
	                LogInfoDic.LOG_MODULAR_ENUM.EFOMS_WORKORDERS_MANAGEMENT.getMessage());
	        return workordersRecordId;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
    
    @Override
    public String insertWorkordersRecord(WorkordersRecordInfo workordersRecord, String workordersStatusCode, 
    		String deadlineDate, String personName) {
    	try {
	    	//修改维修工单状态
	    	String workordersId = workordersRecord.getWorkordersId();
	    	WorkordersInfo workordersInfo = new WorkordersInfo();
	    	workordersInfo.setWorkordersId(workordersId);
	    	workordersInfo.setWorkordersStatusCode(workordersStatusCode);
	    	workordersInfo.setWorkordersStatusName(GetObjectHelper.getNameByStatusCode(workordersStatusCode));
	    	workordersInfo.setUpdateDate(TimeHelper.dateToLongString());
	    	//维修期限时间
	    	workordersInfo.setDeadlineTime(deadlineDate);
	    	
	    	if(EnumDicPublic.ORD_OPER_DELETE.getKey().equals(workordersRecord.getOperTypeCode())) {
	    		//撤销
	    		workordersInfo.setFinishTime(TimeHelper.dateToLongString());
	    	}else if(EnumDicPublic.ORD_OPER_PRESS.getKey().equals(workordersRecord.getOperTypeCode())) {
	        	//催办
	        	WorkordersInfo workorders = workordersInfoMapper.selectById(workordersId);
	        	if(null != workorders && !"".equals(workorders)) {
	        		workordersInfo.setPressTimes(workorders.getPressTimes() + 1 );
	        	}
	    	}else if(EnumDicPublic.ORD_OPER_POSTPONE.getKey().equals(workordersRecord.getOperTypeCode())) {
	    		//延期申请
	    		workordersInfo.setIsDefer(true);
	    	}else if(EnumDicPublic.ORD_OPER_CHECK.getKey().equals(workordersRecord.getOperTypeCode())
	    			|| EnumDicPublic.ORD_OPER_FACKBACK.getKey().equals(workordersRecord.getOperTypeCode())) {
	    		//延期审核 OR 完成反馈
	    		workordersInfo.setIsDefer(false);
	    		//修改所有延期申请记录状态
	    		List<WorkordersRecordInfo> workordersRecordList = this.selectWorkordersRecordList(null, workordersId,
	    				null, EnumDicPublic.OPER_NOHANDLE.getKey(), null, null);
	        	for(WorkordersRecordInfo workordersRecordInfo : workordersRecordList) {
	        		WorkordersRecordInfo workordersRecordInfoU = new WorkordersRecordInfo();
	        		workordersRecordInfoU.setWorkordersRecordId(workordersRecordInfo.getWorkordersRecordId());
	        		workordersRecordInfoU.setOperResultCode(EnumDicPublic.OPER_ISHANDLE.getKey());
	        		workordersRecordInfoU.setOperResult(EnumDicPublic.OPER_ISHANDLE.getValue());
	            	workordersRecordMapper.updateByIdSelective(workordersRecordInfoU);
	        	}
	    	}
	    	workordersInfoMapper.updateByIdSelective(workordersInfo);
	    	//新增维修操作日志
	    	if(null != workordersStatusCode) {
	    		workordersRecord.setEndStatusCode(workordersStatusCode);
	    	}
	    	String result = this.insertWorkordersRecord(workordersRecord, personName);
	    	return result;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }

    @Override
    public String updateWorkordersRecord(WorkordersRecordInfo workordersRecord, String personName){
    	try {
	    	String workordersRecordId = workordersRecord.getWorkordersRecordId();
	    	workordersRecordMapper.updateByIdSelective(workordersRecord);
	    	
	    	//先删除关联附件，后新增附件
	    	fileInfoMapper.deleteFileByReleteId(workordersRecordId);
	        List<FileInfo> fileInfoList = workordersRecord.getFileInfoList();
	    	if(null != fileInfoList && fileInfoList.size() > 0) {
	    		for(FileInfo fileInfo : fileInfoList) {
	        		String fileId = "F" + TimeHelper.getUniqueString();
	        		fileInfo.setFileId(fileId);
	        		fileInfo.setReleteId(workordersRecordId);
	        		fileInfoMapper.insert(fileInfo);
	        	}
	    	}
	        pubLogMapper.saveNoramlLogInfo(personName,null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
	                String.format("修改维修记录编号:{%s} 操作成功!", new Object[] { workordersRecordId }),
	                LogInfoDic.LOG_MODULAR_ENUM.EFOMS_WORKORDERS_MANAGEMENT.getMessage());
	        return workordersRecordId;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }

    @Override
    public String deleteWorkordersRecord(String workordersRecordId, String personName){
    	try {
	    	workordersRecordMapper.deleteById(workordersRecordId);
	        pubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
	                String.format("删除维修记录编号:{%s} 操作成功!", new Object[] { workordersRecordId }),
	                LogInfoDic.LOG_MODULAR_ENUM.EFOMS_WORKORDERS_MANAGEMENT.getMessage());
	        return workordersRecordId;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
}
